/*
 * Copyright (C) 2018 Zhejiang xiaominfo Technology CO.,LTD.
 * All rights reserved.
 * Official Web Site: http://www.xiaominfo.com.
 * Developer Web Site: http://open.xiaominfo.com.
 */

package com.github.xiaoymin.map.request.amap;

import cn.hutool.core.collection.CollectionUtil;
import com.github.xiaoymin.map.common.MapClientConstants;
import com.github.xiaoymin.map.common.em.AmapCoordEnums;
import com.github.xiaoymin.map.exception.MapClientException;
import com.github.xiaoymin.map.model.MapLocation;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 坐标转换
 * https://lbs.amap.com/api/webservice/guide/api/convert
 * @author <a href="mailto:xiaoymin@foxmail.com">xiaoymin@foxmail.com</a>
 * 2020/09/23 16:49
 * @since:map-client 1.0
 */
public class AmapCoordConvertRequest {
    /**
     * 经纬度坐标
     * 传入内容规则：经度在前，纬度在后，经纬度间以“,”分割，经纬度小数点后不要超过 6 位。
     * 如果需要解析多个经纬度的话，请用"|"进行间隔，并且将 batch 参数设置为 true，最多支持传入 20 对坐标点。每对点坐标之间用"|"分割。
     */
    private List<MapLocation> mapLocations=new ArrayList<>();
    /**
     * 转换为目标坐标系
     */
    private AmapCoordEnums amapCoordEnums;

    public AmapCoordConvertRequest(List<MapLocation> mapLocations, AmapCoordEnums amapCoordEnums) {
        this.mapLocations = mapLocations;
        this.amapCoordEnums = amapCoordEnums;
    }
    /**
     * 获取当前对象的map参数
     * @return 参数map
     */
    public Map<String,String> toMap(){
        Map<String,String> parameterMap=new HashMap<>();
        String locations=CollectionUtil.join(mapLocations.stream().map(mapLocation -> mapLocation.getLng()+","+mapLocation.getLat()).collect(Collectors.toList()),MapClientConstants.AMAP_JOIN_ELEMENT_CHAR);
        parameterMap.put("locations",locations);
        parameterMap.put("coordsys",amapCoordEnums.name());
        return parameterMap;
    }


    public static class Builder {

        private List<MapLocation> mapLocations = new ArrayList<>();
        /**
         * 默认不转换
         */
        private AmapCoordEnums amapCoordEnums=AmapCoordEnums.autonavi;
        /**
         * 添加地理位置
         * @param mapLocation 经纬度坐标对象
         * @return 当前Builder对象
         */
        public Builder addLocation(MapLocation mapLocation){
            mapLocations.add(mapLocation);
            return this;
        }

        /**
         * 转换坐标系类型
         * @param coordType 坐标系类型
         * @return 当前Builder对象
         */
        public Builder coordType(AmapCoordEnums coordType){
            this.amapCoordEnums=coordType;
            return this;
        }

        /**
         * 添加地理位置
         * @param lng 经度
         * @param lat 纬度
         * @return 当前Builder对象
         */
        public Builder addLocation(String lng, String lat){
            mapLocations.add(new MapLocation(lng,lat));
            return this;
        }
        /**
         * 构造逆址编码请求对象
         * @return 逆址编码请求对象
         */
        public AmapCoordConvertRequest build(){
            if (CollectionUtil.isEmpty(mapLocations)){
                throw new MapClientException("转换坐标不能为空");
            }
            if (mapLocations.size()>MapClientConstants.AMAP_COORD_EXCHANGE_MAX_SIZE){
                throw new MapClientException("转换坐标不能超过40组");
            }
            return new AmapCoordConvertRequest(mapLocations,amapCoordEnums);
        }

    }
}
